package com.xjk.cloud.dao;

import com.xjk.cloud.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<User> findOne(Long id) {
        return jdbcTemplate.query("select * from user where user_id=?",new Object[]{id}, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                User user = new User();
                user.setId(resultSet.getLong("user_id"));
                user.setAge(resultSet.getInt("age"));
                user.setBalance(resultSet.getBigDecimal("balance"));
                user.setName(resultSet.getString("name"));
                user.setUesrName(resultSet.getString("user_name"));
                return user;
            }
        });
    }

    public User findOne2(Long id) {
        return jdbcTemplate.queryForObject("select * from user where user_id=?",new Object[]{id}, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                User user = new User();
                user.setId(resultSet.getLong("user_id"));
                user.setAge(resultSet.getInt("age"));
                user.setBalance(resultSet.getBigDecimal("balance"));
                user.setName(resultSet.getString("name"));
                user.setUesrName(resultSet.getString("user_name"));
                return user;
            }
        });
//        jdbcTemplate.queryForObject("select * from user where id=?",new Object[]{id}, )
//        return null;
    }
}
